//
//  BLVerticalRoomSingleButton.swift
//  swiftDemo1
//
//  Created by BigL055 on 16/5/30.
//  Copyright © 2016年 bigL.sns.com. All rights reserved.
//

import UIKit
import SnapKit



class BLVerticalRoomSingleButton: UIButton {
    
    var roomID : String = ""
    
    //在线label
    private lazy var onlineLabel = UILabel()
    //主播昵称按钮
    private lazy var userNameBtn = UIButton(type: UIButtonType.Custom)
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    /// 取消高亮
    override var highlighted: Bool{
        willSet{
            
        }
    }
}

//数据设置
extension BLVerticalRoomSingleButton{
    /**
     设置房间数据
     
     - parameter onlineNumber: 直播人数
     - parameter userName:     主播名字
     */
    func setupRoomData(onlineNumber onlineNumber : NSNumber?,
                                    userName : String?,
                                    roomID : String?) {
        //直播人数
        if onlineNumber != nil {
            SetOnlineNumber(Int(onlineNumber!))
        }
        //主播名字
        if userName != nil {
            userNameBtn.setTitle(userName, forState: .Normal)
        }
        //房间ID
        if roomID != nil {
        self.roomID = roomID!
        }
        
    }
    
    /**
     设置在线人数
     
     - parameter onlineNumber: 传入在线人数
     */
    private func SetOnlineNumber(onlineNumber : Int) {
        //设置在线人数Label
        var onLineStr: String  = "\(onlineNumber)在线"
        if Int(onlineNumber) > 10000 {
            let endNumber = CGFloat(onlineNumber) * 0.0001
            onLineStr = String(format: "%0.1f万在线",endNumber)
        }
        onlineLabel.text = onLineStr
    }
    
}

//设置UI
extension BLVerticalRoomSingleButton{
    
    private func setupUI() {
        
        //在线label默认参数配置
        onlineLabel.text = "王人"
        onlineLabel.textColor = UIColor.whiteColor()
        onlineLabel.font = UIFont.systemFontOfSize(10)
        self.addSubview(onlineLabel)
        
        onlineLabel.snp_makeConstraints { (make) in
            make.top.equalToSuperview().offset(4)
            make.right.equalToSuperview().offset(-4)
        }
        
        //阴影图层
        let shadeImage = UIImage(named: "Image_shade_50x44_")
        let shadeView = UIImageView(image: shadeImage)
        shadeView.alpha = 0.8
        self.addSubview(shadeView)
        
        shadeView.snp_makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        //主播昵称按钮默认参数配置
        userNameBtn.setImage(UIImage(named: "Img_user_16x16_" ), forState: .Normal)
        userNameBtn.titleLabel?.font = UIFont.systemFontOfSize(12)
        userNameBtn.adjustsImageWhenDisabled = true
        userNameBtn.sizeToFit()
        self.addSubview(userNameBtn)
        
        userNameBtn.snp_makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-2)
            make.left.equalToSuperview().offset(2)
        }
        
    }
    
}
